package cn.devices.controller;


import cn.devices.dto.InventoryDto;
import cn.devices.entity.TbInventory;
import cn.devices.model.JsonMessage;
import cn.devices.service.TbCodeGenService;
import cn.devices.service.TbInventoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 盘点清单 前端控制器
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/tb-inventory")
public class TbInventoryController {

    @Autowired
    private TbInventoryService tbInventoryService;

    @Autowired
    private TbCodeGenService genService;

    /**
     * 该盘点任务所有盘点到的设备和未盘点的设备
     * priority =1 未扫描到　　等级高　＝2　故障　等级中　＝3　无问题
     *
     * @param taskId
     * @return
     */
    @GetMapping("inventoryTask")
    public JsonMessage inventoryTask(@RequestParam(defaultValue = "1") int pageIndex,
                                     @RequestParam(defaultValue = "10") int pageSize, String taskId) {
        JsonMessage message = new JsonMessage();
        PageInfo<InventoryDto> pageInfo = tbInventoryService.inventoryDt(pageIndex, pageSize, taskId);
        message.setData(pageInfo);
        return message;
    }

    /**
     * 添加盘点清单设备
     *
     * @param tbInventory
     * @return
     */
    @PostMapping("tbInventoryAdd")
    public JsonMessage tbInventoryAdd(@RequestBody TbInventory tbInventory) {
        JsonMessage message = new JsonMessage();
        String a="0";
        tbInventory.setCodeTime(new Date());
        tbInventory.setDeviceStatus(tbInventoryService.status(tbInventory.getDeviceCode()));
        if (tbInventory.getDeviceStatus().equals("2")) {
            tbInventory.setInventoryInfo("设备故障");
            tbInventory.setPriority("2");
        } else {
            tbInventory.setInventoryInfo("设备正常");
            tbInventory.setPriority("3");
        }
        tbInventoryService.tbInventoryAdd(tbInventory);
        message.setMessage("ok");
        return message;
    }

    /**
     * 该盘点任务所有盘点到的设备
     *
     * @param pageIndex
     * @param pageSize
     * @param taskId
     * @return
     */
    @GetMapping("tbInventoryList")
    public JsonMessage tbInventoryList(@RequestParam(defaultValue = "1") int pageIndex,
                                       @RequestParam(defaultValue = "10") int pageSize,
                                       String taskId) {
        JsonMessage message = new JsonMessage();
        PageInfo<TbInventory> pageInfo = tbInventoryService.testInventoryList(pageIndex, pageSize, taskId);
        message.setData(pageInfo);
        return message;
    }

    /**
     * 某一个盘点任务中某个设备的盘点信息
     *
     * @param taskId
     * @param deviceCode
     * @return
     */
    @GetMapping("tbInventoryById")
    public JsonMessage tbInventoryById(String taskId, String deviceCode) {
        JsonMessage message = new JsonMessage();
        TbInventory tbInventory = tbInventoryService.tbInventoryById(taskId, deviceCode);
        message.setData(tbInventory);
        return message;
    }

}
